package com.zycuse.nettyboot;

import com.zycuse.nettyboot.NettyServer.NettyClientTest;
import com.zycuse.nettyboot.NettyServer.NettyServerService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@SpringBootApplication
@RequestMapping("/index")
@RestController
public class NettybootApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(NettybootApplication.class, args);
        applicationContext.getBean(NettyClientTest.class).startClient();
        applicationContext.getBean(NettyServerService.class).startNetty();

    }

    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }

    @ResponseBody
    @RequestMapping("/hello1")
    public void hello1(HttpServletRequest request, HttpServletResponse response) {
        try {
            final ServletOutputStream outputStream  = response.getOutputStream();
            new Thread(()-> {
                try {
                    outputStream.print("fasd");
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping("/hello2")
    public void hello2(HttpServletRequest request, HttpServletResponse response) {
        //final HttpServletResponse response1 = response;
        try {
            response.getOutputStream().print("sdafasdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping("/hello3")
    public void hello3(HttpServletRequest request, HttpServletResponse response) {
        //final HttpServletResponse response1 = response;
        new Thread(()-> {
            try {
                response.getOutputStream().print("fasd");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(()-> {
            try {
                response.getOutputStream().print("fasd");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}
